<?php 
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\InformationPart;
use \PsiFramework\Applications\AlloMediaUser\Classes\Actions\ShopAction;

$order = $response->getAttribute('order');
$currencySymbol = $order->getCurrency()->getSymbol();
$quantities = $response->getAttribute('quantities');
$orderArray = ShopAction::getOrder($order, true);

$entries = $templateManager->getParameter('entries');

$count = 0;
foreach ($entries as $entry) {
	$profile = $entry->getCategory()->getProfile();
	$profile->getParts();
	$parts = $profile->sortParts();

	foreach ($parts as $part) {
		if (!$part->isFree()) {
		    $count++;
		}
	}
}

$itemCount = count($orderArray['items']);

$response->setAttribute('empty', $itemCount==0);
?>
<article id="cont-princ" class="nocols">
    <?php $templateManager->displayTemplate('Steps', $response);  ?>
    <h1><?php Printer::display($languageManager->getText('MY_BASKET')); ?></h1>
    <div id="monPanier" class="fiche">
        <form
            id="basketForm"
            method="POST"
            action="<?php Printer::display(HTML::linkHref('action=ValidateBasket')); ?>">
            <table>
                <thead class="tableTete">
					<tr>
						<th class="col0 tableTitre">#</th>
						<th class="col1 tableTitre"><?php Printer::display($languageManager->getText('PRODUCT')); ?></th>
						<th class="col3 tableTitre"><?php Printer::display($languageManager->getText('SUBSCRIPTION_LENGTH')); ?></th>
						<th class="col4 tableTitre"><?php Printer::display($languageManager->getText('PRICE')); ?></th>
						<th class="col4 tableTitre"></th>
					</tr>
				</thead>
                <tbody>
                    <tr>
    					<td class="sep" colspan="5"></td>
    				</tr>
                    <?php
                    $total = 0;
                    $realTotal = 0;
                    
                    $i = 1 ;
                    foreach ($orderArray['items'] as $item) {
                        $responseId = $item['item'] ;
                    ?>
                    <tr>
                        <td class="col0 tableTexte"><?php Printer::display($i); ?></td>
                        <td class="col1 tableTexte">
                        <?php
                        echo HTML::link(
                            $responseId->getEntry() . ' - ' . $responseId->getTheme(),
                            'action=ShowTheme&entry_id='.$responseId->getEntry()->getid().
                            '&form_id='.$responseId->getTheme()->getId().
                            '&category_id='.$responseId->getEntry()->getCategory()->getId()
                        );
                        $sites = $responseId->getEntry()->getCategory()->getSites();
                        
                        if (!is_null($sites) && count($sites)>0) {
                        ?>
                            <br/>
                            <sub>
                            <?php 
                            foreach($sites as $site){
                                Printer::display($site->getSite()->getName());
                                echo '<br/>';
                            }
                            ?>
                            </sub>
                        <?php 
                        }
                        ?>
                        </td>
                        <td class="col2 tableTexte">
                            <input
                                type="hidden"
                                name="responseId[]"
                                value="<?php Printer::display($responseId->getId()); ?>" />
                            <select name="quantity[]" class="updateQuantity">
                            <?php
                            foreach($quantities as $q=>$qv){
                                $selected = '';
                                if ($item['quantity'] == $q) {
                                   $selected = 'selected="selected"'; 
                                }
                                  
                            ?>
                                <option
                                    value="<?php Printer::display($q); ?>"
                                    <?php echo $selected; ?>
                                ><?php Printer::display($qv['label']); ?></option>
                            <?php    
                            }
                            ?>
                            </select>
                        </td>
                        <td class="col3 tableTexte">
                            <?php Printer::display(number_format($item['realSubTotal'], 2, '.', ' ').$currencySymbol); ?>
                            <?php 
                            if ($item['discountValue']!=0) {
                            ?>
                            <br/>(<s class="oldPrice"><?php Printer::display(number_format($item['subTotal'], 2, '.', ' ').$currencySymbol); ?></s>)
                            <?php
                            }
                            ?>
                        </td>
                        <td class="col4 tableTexte">
                            <?php
    						echo HTML::link(
    					        '',
    					        'action=DeleteBasketItem&order_item_id='.$item['id'],
    					        array(
    				                'class'=>'sup',
    				                'title'=>$languageManager->getText('DELETE_FROM_BASKET')
    			                )
    				        );
    						?>
                        </td>
                    </tr>
                    <tr>
    					<td class="sep" colspan="5"></td>
    				</tr>
                    <?php
                        $i++;
                    }
                    ?>
                </tbody>
                <tfoot class="tablePied">
					<tr>
						<td class="col1 tableTotal" colspan="3"><?php Printer::display($languageManager->getText('TOTAL')); ?> <sub><?php Printer::display($languageManager->getText('WO_TAXES')); ?></sub></td>
						<td class="col4 tableTotal">
						    <?php Printer::display(number_format($orderArray['realTotal'], 2, '.', ' ').$currencySymbol); ?>
						    <?php 
                            if ($orderArray['total'] != $orderArray['realTotal']) {
                            ?>
                            <br/>(<s class="oldPrice"><?php Printer::display(number_format($orderArray['total'], 2, '.', ' ').$currencySymbol); ?></s>)
                            <?php
                            }
                            ?>
					    </td>
						<td class="col5 tableTotal"></td>
					</tr>
				</tfoot>
            </table>
            <fieldset>
                <div>
                    <?php echo HTML::link($languageManager->getText('RETURN_TO_HOMEPAGE'), 'action=Index'); ?>
                </div>
            </fieldset>
            <?php
            if (count($orderArray['items'])>0) {
            ?>
            <fieldset class="formConfirm">
				<div class="actionConfirm">
				    <?php
				    if ($count<$itemCount) {
    				    echo HTML::link(
    			    		$languageManager->getText('BUY_OTHER_OPTIONS'),
    			    		'action=ShowProfileServices'
    				    );
				    }
				    ?>
					<input
                        type="submit"
                        name="submitType"
                        value="<?php Printer::display($languageManager->getText('CHECKOUT')); ?>">
				</div>
			</fieldset>
			<?php
            }
            ?>
        </form>
    </div>
</article>
<script>
    $(document).ready(function(){
        $(".updateQuantity").change(function(){
        	$("#basketForm").submit();
        });
    });
</script>